package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.hzdesk.media.koreabatchmatvector.adapter.TimeLineAdapter;
import com.hzdesk.media.koreabatchmatvector.bean.VideoFile;
import com.hzdesk.media.koreabatchmatvector.custom.HorizontalselectedView;
import com.hzdesk.media.koreabatchmatvector.dao.VideoFileDaoUtils;
import com.hzdesk.media.koreabatchmatvector.logic.contract.TimeLineContract;
import com.hzdesk.media.koreabatchmatvector.utils.DateUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/7/17.
 * 时间轴persenter
 */

public class TimeLinePersenter implements TimeLineContract.ITimeLinePersenter {
    private Context mContext;
    private TimeLineContract.ITimeLineView iTimeLineView;
    private HorizontalselectedView horizontalselectedView;
    private RecyclerView recyclerView;
    private List<VideoFile> list;
    private TimeLineAdapter timeLineAdapter;
    private List<MyTimeLineBean> timeLineBeanList;
    private List<String> yearList;

    public TimeLinePersenter(TimeLineContract.ITimeLineView iTimeLineView) {
        this.iTimeLineView = iTimeLineView;
    }

    @Override
    public void initView(Context context, HorizontalselectedView horizontalselectedView, RecyclerView recyclerView) {
        mContext = context;
        this.horizontalselectedView = horizontalselectedView;
        this.recyclerView = recyclerView;
        VideoFileDaoUtils videoFileDaoUtils = new VideoFileDaoUtils(mContext);
        list = videoFileDaoUtils.queryAllVideoFile();
        timeLineBeanList = new ArrayList<>();
        yearList = new ArrayList<>();

        //存在数据
        if (list.size() > 0) {
            //区分会议
            for (VideoFile videoFile : list) {
                boolean ishas = false;
                String uuid = videoFile.getMeetingGroup();
                long time = videoFile.getCreatetime();
                for (MyTimeLineBean bean : timeLineBeanList) {
                    if (bean.getUuid().equals(uuid)) {
                        ishas = true;
                        break;
                    }
                }
                if (ishas) {
                    continue;
                }
                timeLineBeanList.add(new MyTimeLineBean(time, uuid));
            }
            //正序排序 为了取出正序的年份
            Collections.sort(timeLineBeanList, new TimeLineBeanComparatorUp());

            //年份集合
            for (MyTimeLineBean bean : timeLineBeanList) {
                boolean ishas = false;
                for (String s : yearList) {
                    if (s.equals(DateUtil.getYear(new Date(bean.getTime())) + "")) {
                        ishas = true;
                        break;
                    }
                }
                if (ishas) {
                    continue;
                }
                yearList.add(DateUtil.getYear(new Date(bean.getTime())) + "");
            }
            //倒序排序
            Collections.sort(timeLineBeanList, new TimeLineBeanComparatorDown());

            this.horizontalselectedView.setData(yearList);

            List<MyTimeLineBean> list = new ArrayList<>();

            for (MyTimeLineBean bean : timeLineBeanList) {
                if (yearList.get(yearList.size() - 1).equals(DateUtil.getYear(new Date(bean.getTime())) + "")) {
                    list.add(bean);
                }
            }

            timeLineAdapter = new TimeLineAdapter(mContext, list);
            timeLineAdapter.setOnItemOnClickListener(new TimeLineAdapter.onItemOnClickListener() {
                @Override
                public void onItemClick(String uuid) {
                    iTimeLineView.onItemClick(uuid);
                }
            });

            this.recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            this.recyclerView.setAdapter(timeLineAdapter);

            this.horizontalselectedView.setOnSelectChangeListener(new HorizontalselectedView.onSelectChangeListener() {
                @Override
                public void change(String text) {
                    List<MyTimeLineBean> list = new ArrayList<>();
                    for (MyTimeLineBean bean : timeLineBeanList) {
                        String year = DateUtil.getYear(new Date(bean.getTime())) + "".trim();
                        if (text.equals(year)) {
                            list.add(bean);
                        }
                    }
                    timeLineAdapter.setDate(list);
                }

                @Override
                public void change(int position) {

                }
            });
        } else {//没有数据
            iTimeLineView.showDefaultPage();
        }


    }

    @Override
    public void onClickedLeftImage() {
        iTimeLineView.setAnLeftOffset();

    }

    @Override
    public void onClickedRightImage() {
        iTimeLineView.setAnRightOffset();

    }

    @Override
    public void onClickedBack() {
        iTimeLineView.back();
    }

    @Override
    public void onRefresh() {

    }

    public class MyTimeLineBean {
        private long time;
        private String uuid;

        public MyTimeLineBean(long time, String uuid) {
            this.time = time;
            this.uuid = uuid;
        }

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }

        public int getmonth() {
            return DateUtil.getMonth(new Date(time));
        }

        @Override
        public String toString() {
            return "MyTimeLineBean{" +
                    "time=" + time +
                    ", uuid='" + uuid + '\'' +
                    '}';
        }
    }

    private class TimeLineBeanComparatorDown implements Comparator<MyTimeLineBean> {

        @Override
        public int compare(MyTimeLineBean o1, MyTimeLineBean o2) {
            if (o1.getTime() < o2.getTime()) {
                return 1;
            } else {
                return -1;

            }
        }
    }

    private class TimeLineBeanComparatorUp implements Comparator<MyTimeLineBean> {

        @Override
        public int compare(MyTimeLineBean o1, MyTimeLineBean o2) {
            if (o1.getTime() < o2.getTime()) {
                return -1;
            } else {
                return 1;

            }
        }
    }


}
